package com.atheima.reggie.controller;

import com.atheima.reggie.common.R;
import com.atheima.reggie.common.ValidateUtil;
import com.atheima.reggie.dto.DishDto;
import com.atheima.reggie.entity.Category;
import com.atheima.reggie.entity.Dish;
import com.atheima.reggie.entity.DishFlavor;
import com.atheima.reggie.service.CategoryService;
import com.atheima.reggie.service.DishFlavorService;
import com.atheima.reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @PostMapping
    public R save(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavor(dishDto);
        return R.ok();
    }

    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        Dish dish = dishService.getById(id);
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id",id);
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(list);
        return R.ok().put("data",dishDto);
    }


    @GetMapping("/page")
    public R page(int page,int pageSize,String name) {
        Page<Dish> employeePage = new Page<>(page, pageSize);
        Page<DishDto> dtoPage = new Page<>(page, pageSize);



        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        if(ValidateUtil.isNotEmpty(name)) {
            wrapper.like("name",name);
        }
        wrapper.orderByDesc("update_time");
        dishService.page(employeePage,wrapper);

        BeanUtils.copyProperties(employeePage,dtoPage,"records");
        List<Dish> records = employeePage.getRecords();
        List<DishDto> dtoRecords = new ArrayList<>();
        for (Dish dish : records) {
            DishDto dishDto1 = new DishDto();
            BeanUtils.copyProperties(dish,dishDto1);
            Long categoryId = dish.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            dishDto1.setCategoryName(byId.getName());
            dtoRecords.add(dishDto1);
        }
        dtoPage.setRecords(dtoRecords);

        return R.ok("成功").put("data",dtoPage);
    }

    @GetMapping("/list")
    public R list(Long categoryId) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        if(categoryId != null) {
            wrapper.eq("category_id",categoryId);
        }
        wrapper.eq("status",1);
        List<Dish> list = dishService.list(wrapper);
        List<DishDto> dtoList = new ArrayList<>();
        for (Dish dish : list) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Long id = dish.getId();
            QueryWrapper<DishFlavor> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("dish_id",id);
            List<DishFlavor> dishFlavors = dishFlavorService.list(wrapper1);
            dishDto.setFlavors(dishFlavors);
            dtoList.add(dishDto);
        }
        return R.ok().put("data",dtoList);
    }
}
